<?php
/**
 *
 * 版权所有：顺势网站管理系统<qwadmin.qiawei.com>

 * 日    期：2016-09-20
 * 版    本：1.0.0
 * 功能说明：顾问列表
 *
 **/

namespace Qwadmin\Controller;

class MessageController extends ComController
{
    public function index()
    {
        $p = isset($_GET['p']) ? intval($_GET['p']) : '1';
        $where = 'deleted = 0';
        $message = M('message');
        $pagesize = 10;#每页数量
        $offset = $pagesize * ($p - 1);//计算记录偏移量
        $count = $message->where($where)->count();

        $list = $message->where($where)->limit($offset . ',' . $pagesize)->select();
        $page = new \Think\Page($count, $pagesize);
        $page = $page->show();
        $this->assign('list', $list);
        $this->assign('page', $page);
        $this->display();
    }
    public function edit($id)
    {

        $id = intval($id);
        $message = M('message')->where('id=' . $id)->find();
        if ($message) {
            $this->assign('info', $message);
        } else {
            $this->error('参数错误！');
        }
        $this->display('form');
    }

    public function update($id = 0)
    {

        $id = intval($id);
        $data['name'] = isset($_POST['name']) ? $_POST['name'] : '';
        $data['mobile'] = isset($_POST['mobile']) ? $_POST['mobile'] : false;
        $data['content'] = isset($_POST['content']) ? $_POST['content'] : false;
        if (!$data['name'] or !$data['mobile'] or !$data['content']) {
            $this->error('警告！姓名，电话，留言内容三者为必填项。');
        }
        if ($id) {
            M('message')->data($data)->where('id=' . $id)->save();
            addlog('编辑留言，ID：' . $id);
            $this->success('恭喜！留言编辑成功！');
        } else {
            $this->error('警告！留言只能修改不允许添加。');
        }
    }

    public function del()
    {

        $id = isset($_REQUEST['id']) ? $_REQUEST['id'] : false;
        if ($id) {
            $map = 'id=' . $id;
            if (M('message')->where($map)->save(['deleted'=>1]) !== false) {
                addlog('删除文章，ID：' . $id);
                $this->success('恭喜，留言删除成功！');
            } else {
                $this->error('参数错误！');
            }
        } else {
            $this->error('参数错误！');
        }

    }
    public function export(){

        $bg = isset($_GET['bg']) ? $_GET['bg'] : '';
        $end = isset($_GET['end']) ? $_GET['end'] : "";
        if(empty($bg) || empty($end)){
            $this->error('请选择时间后再导出！');
        }
        $bgTime = $bg." 00:00:00";
        $endTime = $end."  23:59:59";
        $where = "deleted = 0 AND ctime >= '".$bgTime."' AND ctime <= '".$endTime."'";
        $message = M('message');
        $list = $message->where($where)->field("id,name,mobile,content,mail,ctime")->select();
        if(!empty($list)){
            $head = array("序号","姓名","电话","内容","邮件","时间");
            $filename = "用户留言咨询信息".date('Ymd');
            $this->exporttocsv($list,$filename,$head);
        }

    }
}